package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    /**
     * 新增床位
     *
     * @param bedDto
     * @return
     */
    @Override
    public Integer addBed(BedDto bedDto) {

        //对象拷贝
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        //创建人
        bed.setCreateBy(1L);
        //创建时间
        bed.setCreateTime(LocalDateTime.now());
        bed.setUpdateTime(LocalDateTime.now());
        bed.setBedStatus(0);

        int num = bedMapper.countByBedNumber(bed.getBedNumber());
        if (num > 0) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_DUPLTCATE_BED_NUMBER);
        }
        return bedMapper.insert(bed);
    }

    /**
     * 根据id删除床位
     *
     * @param id 床位id
     * @return
     */
    @Override
    public Integer deleteBedById(Long id) {
        bedMapper.deleteById(id);
        return 0;
    }

    /**
     * 修改床位
     *
     * @param bedDto
     * @return
     */
    @Override
    public Integer updateBed(BedDto bedDto) {

        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        bed.setUpdateTime(LocalDateTime.now());
        bed.setUpdateBy(1L);
        if (ObjectUtil.isNotEmpty(bedDto.getBedStatus())) {
            bed.setBedStatus(bedDto.getBedStatus());
        }

        bedMapper.update(bed);
        return 0;
    }

    /**
     * 根据床位id查询
     *
     * @param id
     * @return
     */
    @Override
    public Optional<Bed> selectBedById(Long id) {
        Bed bed = bedMapper.getById(id);
        //Optional 是 Java 8 引入的一个容器类，旨在帮助开发者更好地处理可能为 null 的值
        return Optional.ofNullable(bed);
    }

    /**
     * 根据房间id查询床位
     *
     * @param roomId 房间ID
     * @return
     */
    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return List.of();
    }


}

